********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global ec0 "dT1e10 dT2e10 dT3e10 dT4e10 dT5e10 dT7e10 dT8e10 dT9e10 dT10e10 dT11e10 dT12e10"
global ec4 "dT1e14 dT2e14 dT3e14 dT4e14 dT5e14 dT7e14 dT8e14 dT9e14 dT10e14 dT11e14 dT12e14"
global ed4top "dT1dtop66e14 dT2dtop66e14 dT3dtop66e14 dT4dtop66e14 dT5dtop66e14 dT7dtop66e14 dT8dtop66e14 dT9dtop66e14 dT10dtop66e14 dT11dtop66e14 dT12dtop66e14"
global ed4mid "dT1dmid6633e14 dT2dmid6633e14 dT3dmid6633e14 dT4dmid6633e14 dT5dmid6633e14 dT7dmid6633e14 dT8dmid6633e14 dT9dmid6633e14 dT10dmid6633e14 dT11dmid6633e14 dT12dmid6633e14"
global controls "lncrefin dT*exlnassets dT*excashas"
global postcontrols "lncrefin postexlnassets postexcashas"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans2019, case(lower)
run RO_creditregister tm(2019m2) tm(2020m2) 0
keep bankid borrowerid time disbur

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(e10 e14 e13 e12 dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2019m3)&time<=tm(2020m2)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

run RO_tdummies 2019 12
run RO_tdintera 12 "e10 e14 dtop66e14 dmid6633e14 exlnassets excashas"

gen post=(time>=tm(2019m9))
foreach var of varlist e10 e14 dtop66e14 dmid6633e14 exlnassets excashas {
gen post`var'=post*`var'
}

**# Bookmark (output - columns 1-2-3-4)
est clear
cd $outputdirectory
reghdfe $y $ec0 $controls $regcommands
est save TAB3_C1234
reghdfe $y $ec4 $controls $regcommands
est save TAB3_C1234, append
reghdfe $y $ed4top $ed4mid $controls $regcommands
est save TAB3_C1234, append
reghdfe $y poste14 $postcontrols $regcommands
est save TAB3_C1234, append
cd ..


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "lncdisbur_w199"
global ec0 "dT1e10 dT2e10 dT3e10 dT4e10 dT5e10 dT7e10 dT8e10 dT9e10 dT10e10 dT11e10 dT12e10"
global ec3 "dT1e13 dT2e13 dT3e13 dT4e13 dT5e13 dT7e13 dT8e13 dT9e13 dT10e13 dT11e13 dT12e13"
global ed3top "dT1dtop85e13 dT2dtop85e13 dT3dtop85e13 dT4dtop85e13 dT5dtop85e13 dT7dtop85e13 dT8dtop85e13 dT9dtop85e13 dT10dtop85e13 dT11dtop85e13 dT12dtop85e13"
global controls "lncrefin dT*exlnassets dT*excashas"
global postcontrols "lncrefin postexlnassets postexcashas"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**# Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG TIME DISBUR COM COMUNE_DIP using $inputdirectory/bloans, case(lower)
run RO_creditregister tm(2014m6) tm(2015m6) 0
keep bankid borrowerid time disbur

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid
collapse (sum) disbur, by(bankid borrowerid time)

**# Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(e10 e13 dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**# Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2015, keepus(lncrefin)
keep if _merge!=2
drop _merge

egen id=group(bankid borrowerid)
xtset id time
gen lncdisbur=100*(ln(disbur)-ln(L.disbur))
keep if time>=tm(2014m7)&time<=tm(2015m6)
winsor2 lncdisbur if lncdisbur!=., suffix(_w199) cuts(1 99)

run RO_tdummies 2015 12
run RO_tdintera 12 "e10 e13 dtop85e13 exlnassets excashas"

gen post=(time>=tm(2015m1))
foreach var of varlist e13 dtop85e13 exlnassets excashas {
gen post`var'=post*`var'
}
gen count=1 if time==tm(2015m1)
recode count (.=0)
egen impulse=sum(count), by(id)
winsor2 lncdisbur if lncdisbur!=.&impulse==1, suffix(_2w199) cuts(1 99)

**# Bookmark (output - columns 5-6-7-8)
est clear
cd $outputdirectory
reghdfe $y $ec0 $controls $regcommands
est save TAB3_C5678
reghdfe $y $ec3 $controls $regcommands
est save TAB3_C5678,append
reghdfe $y $ed3top $controls $regcommands
est save TAB3_C5678, append
reghdfe lncdisbur_2w199 poste13 $postcontrols if impulse==1 $regcommands
est save TAB3_C5678, append
cd ..


